package com.kara.webapp.components.clazz;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.Select;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.EnumValueEncoder;
import org.appfuse.model.LabelValue;

import com.kara.entity.Clazz;
import com.kara.entity.SchoolYear;
import com.kara.entity.Trinn;
import com.kara.service.ClazzManager;
import com.kara.webapp.util.LabelValueSelectModel;

public class ClazzForm implements ClientElement {

	@Parameter(required = true)
	@Property
	private Clazz clazz;

	@Persist
	@Property
	private LabelValue labelValue;

	@Component(id = "form")
	private Form form;

	@Component(id = "name", parameters = { "value=clazz.name", "validate=required" })
	private TextField name;

	@Component(id = "trinnField", parameters = { "value=clazz.trinn", "model=trinnModell", "encoder=trinnEncoder", "blankOption=NEVER" })
	private Select trinnField;

	@Component(id = "yearField", parameters= {"value=labelValue", "model=yearModell", "blankOption=NEVER" })
	private Select yearField;

	@Parameter(value = "message:button.save", defaultPrefix = BindingConstants.LITERAL)
	@Property
	private String submitLabel;
	
	@Inject
	private ClazzManager clazzManager;

	public String getClientId() {
		return form.getClientId();
	}

	public List<Trinn> getTrinnModell() {
		return Arrays.asList(Trinn.values());
	}

	public ValueEncoder<Trinn> getTrinnEncoder() {
		return new EnumValueEncoder<Trinn>(Trinn.class);
	}
	
	public SelectModel getYearModell() {
		List<SchoolYear> allSchoolYears = clazzManager.getAllSchoolYears();
		List<LabelValue> lb = new ArrayList<LabelValue>();
		
		for (SchoolYear a : allSchoolYears) {
			lb.add(new LabelValue(a.getStartDate().toString(), a.getId().toString()));
		}
		return new LabelValueSelectModel<LabelValue>(lb); 
	}
	
	public ValueEncoder<LabelValue> getYearEncoder() {
		return encoder;
	}

	private final ValueEncoder<LabelValue> encoder = new ValueEncoder<LabelValue>() {
		
		public LabelValue toValue(String clientValue) {
			SchoolYear schoolYears = clazzManager.getSchoolYears(Long.parseLong(clientValue));
			return new LabelValue(schoolYears.getStartDate().toString(),schoolYears.getId().toString());
		}

		public String toClient(LabelValue value) {
			return value.getValue().toString();
		}
	};

	Object onSuccess() {
	    SchoolYear schoolYear = clazzManager.getSchoolYears(Long.valueOf(labelValue.getValue()));
	    clazz.setSchoolYear(schoolYear);
		clazz = clazzManager.saveClazz(clazz);
		return null;
	}
}
